# -*- coding: utf-8 -*-
from FileUtils import all_file_path
import os

class FilterLog(object):
    '''
    从系统log中过滤出错误信息
    '''
    def __init__(self,file_path):
        self.all_result_path=file_path

    def start(self):
        '''
        通过系统的log,生成filter_log
        :return:
        '''
        for log_file in self.__log__file(self.all_result_path,'.log'):
            self.analyze(log_file)

    def __log__file(self, all_path_result,the_suffix_name):
        return all_file_path(all_path_result,the_suffix_name).values()

    def analyze(self, log_file):
        """
        过滤Exception到log文件夹内
        :param log_file: log的路径
        :return:
        """
        errorId=0
        go_on_id=0
        log_filter_name=os.path.split(log_file)[1].split('.')[0]
        print(log_filter_name)
        with open(self.all_result_path+'/log/{}filter.log'.format(log_filter_name),'w') as s:

            with open(log_file,encoding='utf-8') as f:
                for line in f:
                    if ('Exception' in line) or ('FATAL' in line) or ('ERROR' in line) or ('Failed' in line) :
                        go_on_id=1
                        s.write('#'+'-'*40+'\n')
                        s.write(line)
                        errorId = line.split('(')[1].split(')')[0].strip()
                    elif go_on_id==1:
                        if errorId in line:
                            s.write(line)
                        else:
                            go_on_id=0



if __name__ == '__main__':
    from collections import OrderedDict
    captureThread= OrderedDict([('search.log', 'E:/python/project/FFAutoTest/TestResult/2018-04-25_10_27_1453/log/search.log')])
    print(captureThread.values())
    # print(('a' in 'b23' )or ('b' in 'b23'))

    